package org.yocastlong.mybatisplus.initscript;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.yocastlong.mybatisplus.db.dao.GoodsSnapshotDao;
import org.yocastlong.mybatisplus.db.dao.GoodsTemplateDao;
import org.yocastlong.mybatisplus.db.dao.OrderDao;
import org.yocastlong.mybatisplus.db.dao.UserDao;
import org.yocastlong.mybatisplus.db.domain.GoodsSnapshotDO;
import org.yocastlong.mybatisplus.db.domain.GoodsTemplateDO;
import org.yocastlong.mybatisplus.db.domain.OrderDO;
import org.yocastlong.mybatisplus.db.domain.UserDO;
import org.yocastlong.mybatisplus.pojo.bo.ThreeLevelAreaBO;
import org.yocastlong.mybatisplus.service.AreaService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 朱海龙
 * 2025/6/6 17:31
 */
@Slf4j
@SpringBootTest
public class InsertInitData {


    @Resource
    private UserDao userDao;

    @Resource
    private OrderDao orderDao;

    @Resource
    private GoodsSnapshotDao goodsSnapshotDao;

    @Resource
    private GoodsTemplateDao goodsTemplateDao;

    @Autowired
    private AreaService areaService;


    @Test
    public void initInsertData() {

        generateOrder();

    }

    private void generateOrder() {

        List<UserDO> users = userDao.list();
        List<ThreeLevelAreaBO> threeLevelAreaBOS = areaService.threeLevelAreaInfoBOList();

        Random random = new Random();
        String[] names = {"手机", "笔记本电脑", "平板电脑", "智能手表", "耳机"};

        List<GoodsSnapshotDO> list = goodsSnapshotDao.list();

        for (GoodsSnapshotDO goodsSnapshotDO : list) {

            OrderDO orderDO = new OrderDO();
            orderDO.setGoodsId(goodsSnapshotDO.getId());
            orderDO.setGoodsName(goodsSnapshotDO.getName());
            orderDO.setGoodsSnapshotVersion(goodsSnapshotDO.getVersion());
            orderDO.setGoodsPrice(goodsSnapshotDO.getCurrentPrice());

            int index = random.nextInt(users.size());

            UserDO userDO = users.get(index);
            orderDO.setUserId(userDO.getId());
            orderDO.setUsername(userDO.getAccount());

            orderDO.setAreaCode(goodsSnapshotDO.getAreaCode());
            orderDO.setAreaName(goodsSnapshotDO.getAreaName());

            orderDao.save(orderDO);

        }


    }

    private void generateGoodsTemplate() {

        List<UserDO> users = userDao.list();
        List<ThreeLevelAreaBO> threeLevelAreaBOS = areaService.threeLevelAreaInfoBOList();

        Random random = new Random();
        String[] names = {"手机", "笔记本电脑", "平板电脑", "智能手表", "耳机"};

        for (int i = 0; i < names.length; i++) {
            GoodsTemplateDO goods = new GoodsTemplateDO();
            goods.setId("G" + String.format("%04d", i + 1));
            goods.setName(names[random.nextInt(names.length)] + " " + (i + 1));
            goods.setVersion(random.nextInt(3) + 1); // 版本1-3

            double rawPrice = 1000 + random.nextDouble() * 10000;


            goods.setRawPrice(String.format("%.2f", rawPrice));

            int index = random.nextInt(users.size());

            UserDO userDO = users.get(index);
            goods.setCreateUserId(userDO.getId());
            goods.setCreateUsername(userDO.getAccount());

            index = random.nextInt(threeLevelAreaBOS.size());
            ThreeLevelAreaBO threeLevelAreaBO = threeLevelAreaBOS.get(index);
            goods.setAreaCode(threeLevelAreaBO.getThreeLevelAreaCode());
            goods.setAreaName(threeLevelAreaBO.getThreeLevelAreaName());

            goodsTemplateDao.save(goods);

            GoodsSnapshotDO goodsSnapshotDO=new GoodsSnapshotDO();
            BeanUtils.copyProperties(goods, goodsSnapshotDO);
            goodsSnapshotDO.setId(null);
            double discount = 0.8 + random.nextDouble() * 0.2; // 8折-10折
            double currentPrice = rawPrice * discount;
            goodsSnapshotDO.setCurrentPrice(String.format("%.2f", currentPrice));
            goodsSnapshotDao.save(goodsSnapshotDO);

        }

    }

    private void generateUserList() {
        List<UserDO> userDOList = new ArrayList<UserDO>();
        UserDO user = new UserDO();
        user.setId("1");
        user.setAccount("18738698707");
        user.setPassword("123456");
        user.setPhone("18738698707");
        user.setUsername("朱海龙");
        userDOList.add(user);

        user = new UserDO();
        user.setId("2");
        user.setAccount("13022105162");
        user.setPassword("123456");
        user.setPhone("13022105162");
        user.setUsername("张震");
        userDOList.add(user);

        boolean b = userDao.saveBatch(userDOList);
        log.error("保存结果=》{}", b);
    }

}















































